Animation
能夠控制動畫在每一幀的動作並指派給動畫元件並透過腳本控製播放
Animator
public Animator aniplay;
待更
加入移動動畫
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class playcontrol : MonoBehaviour
{
public float movechage = 50f;
Rigidbody2D rigid2D;
[SerializeField]
private float speed;
[SerializeField]
private float speed_x_constraint;
public Animator aniplay;
public int jumpcheck=0;
private Vector2 moveVelocity;
void Start()
{
rigid2D = this.gameObject.GetComponent<Rigidbody2D>();
}
void Update()
{
float deltatime = Time.deltaTime;
bool iswalking = false;
//跳躍
if (Input.GetKeyDown(KeyCode.Space) && jumpcheck<2)
{
rigid2D.AddForce(new Vector2(0, 200), ForceMode2D.Impulse);
jumpcheck +=1;
}
//右移動
if (Input.GetKey(KeyCode.D))
{
iswalking = true;
rigid2D.velocity = new Vector2(speed_x_constraint, rigid2D.velocity.y);
}
//左移動
if(Input.GetKey(KeyCode.A))
{
iswalking = true;
rigid2D.velocity = new Vector2(-speed_x_constraint, rigid2D.velocity.y);
}
//移動動畫
if (iswalking)
{
if (aniplay.GetInteger("startw") == 0)
aniplay.SetInteger("startw", 1);
}
else
{
if(aniplay.GetInteger("startw") == 1)
aniplay.SetInteger("startw", 0);
}
}
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "trap") {
hp -= 1;
rigid2D.AddForce(new Vector2(0, 100), ForceMode2D.Impulse);
}
if (coll.gameObject.tag == "Scenesobject") {
jumpcheck = 0;
}
}
}
參考資料:
https://www.youtube.com/watch?v=aIOYDtJnJI0